package com.sthz.facepay.mapper;

import com.sthz.facepay.model.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface UserMapper {
    @Delete({
        "delete from st_user",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into st_user (id, stid, ",
        "name, username, ",
        "password, sex, phone, ",
        "role, state, picture, ",
        "business_name, receiver, ",
        "card)",
        "values (#{id,jdbcType=INTEGER}, #{sid,jdbcType=INTEGER}, ",
        "#{name,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, ",
        "#{password,jdbcType=VARCHAR}, #{sex,jdbcType=TINYINT}, #{phone,jdbcType=VARCHAR}, ",
        "#{role,jdbcType=INTEGER}, #{state,jdbcType=TINYINT}, #{picture,jdbcType=VARCHAR}, ",
        "#{business_name,jdbcType=VARCHAR}, #{receiver,jdbcType=VARCHAR}, ",
        "#{card,jdbcType=VARCHAR})"
    })
    int insert(User record);

    @InsertProvider(type=UserSqlProvider.class, method="insertSelective")
    int insertSelective(User record);

    @Select({
        "select",
        "id, stid, name, username, password, sex, phone, role, state, picture, business_name, ",
        "receiver, card",
        "from st_user",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="stid", property="stid", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="username", property="username", jdbcType=JdbcType.VARCHAR),
        @Result(column="password", property="password", jdbcType=JdbcType.VARCHAR),
        @Result(column="sex", property="sex", jdbcType=JdbcType.TINYINT),
        @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
        @Result(column="role", property="role", jdbcType=JdbcType.INTEGER),
        @Result(column="state", property="state", jdbcType=JdbcType.TINYINT),
        @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
        @Result(column="business_name", property="business_name", jdbcType=JdbcType.VARCHAR),
        @Result(column="receiver", property="receiver", jdbcType=JdbcType.VARCHAR),
        @Result(column="card", property="card", jdbcType=JdbcType.VARCHAR)
    })
    User selectByPrimaryKey(Integer id);

    @UpdateProvider(type=UserSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(User record);

    @Update({
        "update st_user",
        "set stid = #{stid,jdbcType=INTEGER},",
          "name = #{name,jdbcType=VARCHAR},",
          "username = #{username,jdbcType=VARCHAR},",
          "password = #{password,jdbcType=VARCHAR},",
          "sex = #{sex,jdbcType=TINYINT},",
          "phone = #{phone,jdbcType=VARCHAR},",
          "role = #{role,jdbcType=INTEGER},",
          "state = #{state,jdbcType=TINYINT},",
          "picture = #{picture,jdbcType=VARCHAR},",
          "business_name = #{business_name,jdbcType=VARCHAR},",
          "receiver = #{receiver,jdbcType=VARCHAR},",
          "card = #{card,jdbcType=VARCHAR}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(User record);

    @Select({
            "select",
            "id, stid, name, username, password, sex, phone, role, state, picture, business_name, ",
            "receiver, card",
            "from st_user",
            "where name = #{name,jdbcType=VARCHAR} and password = #{password,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="stid", property="stid", jdbcType=JdbcType.INTEGER),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="username", property="username", jdbcType=JdbcType.VARCHAR),
            @Result(column="password", property="password", jdbcType=JdbcType.VARCHAR),
            @Result(column="sex", property="sex", jdbcType=JdbcType.TINYINT),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column="role", property="role", jdbcType=JdbcType.INTEGER),
            @Result(column="state", property="state", jdbcType=JdbcType.TINYINT),
            @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
            @Result(column="business_name", property="business_name", jdbcType=JdbcType.VARCHAR),
            @Result(column="receiver", property="receiver", jdbcType=JdbcType.VARCHAR),
            @Result(column="card", property="card", jdbcType=JdbcType.VARCHAR)
    })
    // 验证用户、密码是否正确
    boolean checkUser(String name, String password);

    @Select({
            "select",
            "id, stid, name, username, password, sex, phone, role, state, picture, business_name, ",
            "receiver, card",
            "from st_user",
            "where name = #{name,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="stid", property="stid", jdbcType=JdbcType.INTEGER),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="username", property="username", jdbcType=JdbcType.VARCHAR),
            @Result(column="password", property="password", jdbcType=JdbcType.VARCHAR),
            @Result(column="sex", property="sex", jdbcType=JdbcType.TINYINT),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column="role", property="role", jdbcType=JdbcType.INTEGER),
            @Result(column="state", property="state", jdbcType=JdbcType.TINYINT),
            @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
            @Result(column="business_name", property="business_name", jdbcType=JdbcType.VARCHAR),
            @Result(column="receiver", property="receiver", jdbcType=JdbcType.VARCHAR),
            @Result(column="card", property="card", jdbcType=JdbcType.VARCHAR)
    })
    // 验证用户是否存在
    User getUserByLoginName(String name);

    @Update({
            "update st_user",
            "set stid = #{stid,jdbcType=INTEGER},",
            "name = #{name,jdbcType=VARCHAR},",
            "username = #{username,jdbcType=VARCHAR},",
            "password = #{password,jdbcType=VARCHAR},",
            "sex = #{sex,jdbcType=TINYINT},",
            "phone = #{phone,jdbcType=VARCHAR},",
            "role = #{role,jdbcType=INTEGER},",
            "state = #{state,jdbcType=TINYINT},",
            "picture = #{picture,jdbcType=VARCHAR},",
            "business_name = #{business_name,jdbcType=VARCHAR},",
            "receiver = #{receiver,jdbcType=VARCHAR},",
            "card = #{card,jdbcType=VARCHAR}",
            "where id = #{id,jdbcType=INTEGER}"
    })
    boolean updateUserById(User user);

    @Update({
            "update st_user ",
            "set password = #{password,jdbcType=VARCHAR} ",
            "where id = #{id,jdbcType=INTEGER}"
    })
    /**
     * 修改密码
     */
    boolean updateUserByPassword(int id, String password);
}